package com.example.controller.usr;

import com.example.context.PageResult;
import com.example.entity.UserAddressDO;
import com.example.service.usr.UserAddressService;
import com.example.vo.addr.AddrResp;
import com.example.vo.addr.UserAddressPageReq;
import com.example.vo.addr.UserAddressReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户地址相关", description = "用户地址相关")
@RestController
@RequestMapping("/api/usr/address")
public class UsrAddressController {

    @Resource
    private UserAddressService addressService;


    @GetMapping("/default")
    @Operation(summary = "获取默认地址", method = "GET")
    public AddrResp getDefaultAddress() {
        return addressService.getDefaultAddress();
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户地址列表", method = "GET")
    public PageResult<AddrResp> getAddressList(@Valid UserAddressPageReq req) {
        return addressService.getAddressList(req);
    }

    @GetMapping("/{id}")
    @Operation(summary = "设置默认地址", method = "GET",
            parameters = {
                    @Parameter(name = "id", description = "地址ID", required = true, in = ParameterIn.PATH)
            })
    public void setDefault(@PathVariable Long id) {
        addressService.setDefault(id);
    }

    @GetMapping("/detail/{addressId}")
    @Operation(summary = "获取用户地址详情", method = "GET"
            , parameters = {
            @Parameter(name = "addressId", description = "地址ID", required = true, in = ParameterIn.PATH)
    })
    public UserAddressDO getAddressById(@PathVariable Long addressId) {
        return addressService.getAddressById(addressId);
    }

    @PostMapping
    @Operation(summary = "添加用户地址", method = "POST")
    public void addAddress(@Valid UserAddressReq req) {
        addressService.addAddress(req);
    }

    @PutMapping
    @Operation(summary = "更新用户地址", method = "PUT")
    public void updateAddress(@Valid UserAddressReq req) {
        addressService.updateAddress(req);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户地址", method = "DELETE"
            , parameters = {
            @Parameter(name = "id", description = "地址ID", required = true, in = ParameterIn.PATH)
    })
    public void deleteAddress(@PathVariable Long id) {
        addressService.deleteAddress(id);
    }
}
